package com.fetch.ui;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;

import com.download.utils.DownloadHelper;
import com.fetch.listener.AboutActionListener;
import com.fetch.listener.DownloadActionListener;
import com.fetch.listener.FetchFileActionListener;
import com.fetch.listener.FetchGraphActionListener;
import com.fetch.listener.FileBrowserActionListener;
import com.fetch.listener.OpenFileActionListener;
import com.fetch.listener.OpenFolderActionListener;
import com.fetch.listener.ProgressBarChangeListener;
import com.fetch.listener.UrlFocusAdapter;

public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    public static final int DEFAULT_WIDTH = 452;
    public static final int DEFAULT_HEIGHT = 200;
    private String iconPath = "../../../images/logo.png";
    //界面元素
    private JPanel panel;
    private JLabel urlLabel;
    private JTextField url;
    private JLabel fileNameLabel;
    private JTextField fileName;
    private JLabel dirLabel;
    private JTextField directory;
    private final JButton fileBrowser;
    private JButton download;
    private JLabel processLabel;
    private final JProgressBar progressBar;
    private final JLabel percentLabel;
    private final JButton openFile;
    private final JButton openFolder;
    
    //监听器
    private ActionListener fileBrowserActionListener;
    private ActionListener downloadActionListener;
    private ActionListener openFileActionListener;
    private ActionListener openFolderActionListener;
    private ActionListener fetchFileActionListener;
    private ActionListener fetchGraphActionListener;
    private ActionListener aboutActionListener;
    private ChangeListener progressBarChangeListener;
    private FocusListener urlFocusAdapter;

    public MainFrame() {
        
        panel = new JPanel();
        urlLabel = new JLabel("下载地址：");
        url = new JTextField();
        fileNameLabel = new JLabel("文件名称：");
        fileName = new JTextField();
        dirLabel = new JLabel("存放目录：");
        directory = new JTextField();
        fileBrowser = new JButton("浏览");
        download = new JButton("下载");
        processLabel = new JLabel("当前进度：");
        progressBar = new JProgressBar();
        percentLabel = new JLabel("0%");
        openFile = new JButton("打开");
        openFolder = new JButton("文件夹");
        
        fileBrowserActionListener = new FileBrowserActionListener(fileBrowser, directory);
        downloadActionListener = new DownloadActionListener(url, fileName, directory, progressBar, openFile, openFolder);
        openFileActionListener = new OpenFileActionListener(directory, fileName);
        openFolderActionListener = new OpenFolderActionListener(directory, fileName);
        fetchFileActionListener = new FetchFileActionListener();
        fetchGraphActionListener = new FetchGraphActionListener();
        aboutActionListener = new AboutActionListener();
        progressBarChangeListener = new ProgressBarChangeListener(progressBar, percentLabel);
        urlFocusAdapter = new UrlFocusAdapter(url,fileName);
        
        setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource(iconPath)));
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        setTitle("下载");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        panel.setPreferredSize(new Dimension(435, 300));
//        panel.setBounds(new Rectangle(0, 0, 800, 400));
        getContentPane().add(panel, BorderLayout.EAST);
        
        urlLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        urlLabel.setLabelFor(url);
        panel.add(urlLabel);

        url.addFocusListener(urlFocusAdapter);
        url.setText("https://codeload.github.com/woshihoujinxin/ticket/zip/master");
        url.setHorizontalAlignment(SwingConstants.LEFT);
        url.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        url.setColumns(32);
        url.setBounds(new Rectangle(146, 10, 360, 21));
        url.setPreferredSize(new Dimension(360, 21));
        panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        panel.add(url);

        fileNameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        fileNameLabel.setLabelFor(fileName);
        panel.add(fileNameLabel);

        fileName.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        fileName.setColumns(32);
        fileName.setBounds(new Rectangle(146, 10, 360, 21));
        fileName.setPreferredSize(new Dimension(360, 21));
        fileNameLabel.setLabelFor(fileName);
        panel.add(fileName);

        dirLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        dirLabel.setLabelFor(directory);
        panel.add(dirLabel);

        directory.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        directory.setColumns(20);
        directory.setBounds(new Rectangle(146, 10, 277, 21));
        directory.setPreferredSize(new Dimension(277, 21));
        directory.setText(DownloadHelper.FILE_PATH);
        dirLabel.setLabelFor(directory);
        panel.add(directory);
        
        fileBrowser.addActionListener(fileBrowserActionListener);
        fileBrowser.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        fileBrowser.setPreferredSize(new Dimension(60, 25));
        fileBrowser.setActionCommand("浏览");
        panel.add(fileBrowser);
        
        download.addActionListener(downloadActionListener);
        download.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        download.setPreferredSize(new Dimension(60, 25));
        panel.add(download);

        processLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        processLabel.setLabelFor(progressBar);
        panel.add(processLabel);
        
        progressBar.addChangeListener(progressBarChangeListener);
        progressBar.setPreferredSize(new Dimension(170, 25));
        progressBar.setMinimum(0);
        progressBar.setMaximum(100);
        progressBar.setValue(0);
        panel.add(progressBar);
        
        percentLabel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        percentLabel.setPreferredSize(new Dimension(35, 15));
        percentLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        panel.add(percentLabel);
        
        openFile.setEnabled(false);
        openFile.addActionListener(openFileActionListener);
        openFile.setPreferredSize(new Dimension(60, 25));
        panel.add(openFile);
        
        openFolder.setEnabled(false);
        openFolder.addActionListener(openFolderActionListener);
        openFolder.setPreferredSize(new Dimension(76, 25));
        panel.add(openFolder);
        
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        
        JMenu model = new JMenu("模式");
        menuBar.add(model);
        
        JMenuItem fetchFile = new JMenuItem("文件模式");
        fetchFile.addActionListener(fetchFileActionListener);
        model.add(fetchFile);
        
        JMenuItem fetchGraph = new JMenuItem("抓图模式");
        fetchGraph.addActionListener(fetchGraphActionListener);
        model.add(fetchGraph);
        
        JMenu other = new JMenu("其他");
        menuBar.add(other);
        
        JMenuItem about = new JMenuItem("关于");
        about.addActionListener(aboutActionListener);
        other.add(about);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setResizable(false);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
